package com.zxx.casserver.modules.org.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxx.cascommon.base.exception.ParamsException;
import com.zxx.cascommon.base.result.Result;
import com.zxx.cascommon.base.result.ResultUtils;
import com.zxx.casserver.modules.org.entity.OrgDO;
import com.zxx.casserver.modules.org.entity.OrgVO;
import com.zxx.casserver.modules.org.service.IOrgService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 组织机构表 前端控制器
 * </p>
 *
 * @author zhoukai
 * @since 2022-07-14
 */
@RestController
@RequestMapping("/org")
public class OrgController {

    private final IOrgService orgService;

    public OrgController(IOrgService orgService) {
        this.orgService = orgService;
    }

    @PostMapping("/list")
    public Result listOrg(@RequestBody OrgVO orgVO) {
        IPage<OrgDO> orgBO = orgService.listOrg(orgVO);
        if (ObjectUtils.isEmpty(orgBO)) {
            return ResultUtils.fail(new JSONObject());
        }
        return ResultUtils.success(orgService.dispose(orgBO));
    }


    @PostMapping("/save")
    public Result saveOrg(@RequestBody OrgVO orgVO) {
        if (ObjectUtils.isEmpty(orgVO.getName())) {
            throw new ParamsException();
        }
        if (ObjectUtils.isEmpty(orgVO.getAppId())) {
            throw new ParamsException();
        }
        if (ObjectUtils.isEmpty(orgVO.getPlatformId())) {
            throw new ParamsException();
        }
        if (ObjectUtils.isEmpty(orgVO.getParentId())) {
            throw new ParamsException();
        }
        OrgDO orgBO = orgService.saveOrg(orgVO);
        if (ObjectUtils.isEmpty(orgBO)) {
            return ResultUtils.fail(new JSONObject());
        }
        return ResultUtils.success(orgBO);
    }


    @PostMapping("/update")
    public Result updateOrg(@RequestBody OrgVO orgVO) {
        if (ObjectUtils.isEmpty(orgVO.getOrgId())) {
            throw new ParamsException();
        }
        if (ObjectUtils.isEmpty(orgVO.getAppId())) {
            throw new ParamsException();
        }
        OrgDO orgBO = orgService.updateOrg(orgVO);
        if (ObjectUtils.isEmpty(orgBO)) {
            return ResultUtils.fail(new JSONObject());
        }
        return ResultUtils.success(orgBO);
    }


    @PostMapping("/delete")
    public Result deleteOrg(@RequestBody OrgVO orgVO) {
        if (ObjectUtils.isEmpty(orgVO.getOrgId())) {
            throw new ParamsException();
        }
        if (ObjectUtils.isEmpty(orgVO.getAppId())) {
            throw new ParamsException();
        }
        Boolean isDelete = orgService.deleteOrg(orgVO);
        if (ObjectUtils.isEmpty(isDelete)) {
            return ResultUtils.fail(false);
        }
        return ResultUtils.success(true);
    }


    @PostMapping("/getOrgTree")
    public Result getOrgTree(@RequestBody OrgVO orgVO) {
        List<OrgDO> orgList = orgService.getOrgTree(orgVO);
        if (ObjectUtils.isEmpty(orgList)) {
            return ResultUtils.fail(new JSONArray());
        }
        return ResultUtils.success(orgList);
    }

}

